package cn.edu.scnu.controller;

import cn.edu.scnu.common.MD5Util;
import cn.edu.scnu.entity.Flower;
import cn.edu.scnu.entity.Myorder;
import cn.edu.scnu.entity.TbMember;
import cn.edu.scnu.service.FlowerService;
import cn.edu.scnu.service.MemberService;
import cn.edu.scnu.service.OrderSevice;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.*;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private FlowerService flowerService;
    @Resource
    private MemberService memberService;

    @Resource
    private OrderSevice orderSevice;

    @RequestMapping("/")
    public String home() {
        return "redirect:/index";
    }
    @RequestMapping("/index")
    public String index(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                        @RequestParam(name = "fname", defaultValue = "")String fname,
                        @RequestParam(name = "fclass", defaultValue = "") String fclass,
                        Integer minprice,
                        Integer maxprice,
                        Model model) {
        Integer pageSize = 8;
        if(minprice==null){minprice=0;}
        if(maxprice==null){maxprice=Integer.MAX_VALUE;}
        Map<String, Object> map = flowerService.queryPage(fname,fclass,minprice,maxprice,pageNo, pageSize);
        int totalRecords = (Integer) map.get("count");
        System.out.println(totalRecords);
        List<Flower> flowerList = (List<Flower>) map.get("recourds");
        // 计算总页数
        Integer pageCount = (totalRecords % pageSize == 0) ? (totalRecords / pageSize) : ((totalRecords / pageSize) + 1);
        model.addAttribute("currentPage", pageNo);
        model.addAttribute("pageCount", pageCount);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("fname", fname);
        model.addAttribute("fclass", fclass);
        model.addAttribute("minprice", minprice);
        model.addAttribute("maxprice", maxprice);
        model.addAttribute("flowerlist", flowerList);
        model.addAttribute("fclasses", flowerService.findfclass());
        return "index";
    }

    @RequestMapping("/index/flowerdetail")
    public String flowerdetail(String flowerid,Model model){
        model.addAttribute("flower",flowerService.findFlowerById(flowerid));
        List<Myorder>orders = orderSevice.findOrdersByflowerId(flowerid);
        model.addAttribute("orders",orders);
        return "flowerdetail";
    }

    @RequestMapping("/index/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/index/logOut")
    public String logOut(HttpSession session){
        session.removeAttribute("memberLogin");
        return "redirect:/index";
    }

    @RequestMapping("/index/toRegister")
    public  String toRegister(){
        return "register";
    }

    @RequestMapping("/index/doLogin")
    @ResponseBody
    public String doLogin(String email, String password, HttpSession session) {
        TbMember member = memberService.login(email, password);

        if (member != null) {
            member.setPassword(""); //去敏
            session.setAttribute("memberLogin", member);
            return "登录成功!";
        } else {
            return "登录失败!";
        }
    }
    @RequestMapping("/index/doRegister")
    @ResponseBody
    public String doRegister(TbMember tbMember) {
        TbMember member = memberService.getById(tbMember.getEmail());

        if (member != null) {

            return "该账号已被注册！";
        }
        tbMember.setPassword(MD5Util.md5(tbMember.getPassword()));
        memberService.save(tbMember);
        return "注册成功！";
    }
}

